package com.example.ymm.modules1.controller.ms;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.ymm.exception.YmException;
import com.example.ymm.model.CommonResult;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.controller.BaseController;
import com.example.ymm.modules1.entity.ms.TProviderPartner;
import com.example.ymm.modules1.entity.ms.VMaterial;
import com.example.ymm.modules1.entity.my.ms.MsMouldDict;
import com.example.ymm.modules1.entity.my.ms.MsMouldItem;
import com.example.ymm.modules1.entity.my.ms.MsMouldMain;
import com.example.ymm.modules1.query.ms.TProviderPartnerQueryCriteria;
import com.example.ymm.modules1.query.ms.VMaterialQueryCriteria;
import com.example.ymm.modules1.service.dto.ImportMsOrderExcelDto;
import com.example.ymm.modules1.service.dto.ms.DbMsOrderDto;
import com.example.ymm.modules1.service.dto.ms.MsMouldDictQueryCriteria;
import com.example.ymm.modules1.service.dto.ms.MsMouldItemQueryCriteria;
import com.example.ymm.modules1.service.dto.ms.MsMouldMainQueryCriteria;
import com.example.ymm.modules1.service.dto.ms.MsOrderDto;
import com.example.ymm.modules1.service.dto.ms.MsOrderListQueryCriteria;
import com.example.ymm.modules1.service.ms.MsMouldDictService;
import com.example.ymm.modules1.service.ms.MsMouldItemService;
import com.example.ymm.modules1.service.ms.MsMouldMainService;
import com.example.ymm.modules1.service.ms.TProviderPartnerService;
import com.example.ymm.modules1.service.ms.VMaterialService;
import com.example.ymm.util.UtilValidate;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("/hl/sys/ms/mould")
@Api(tags = "ms费用用管理")
@Slf4j
public class MsSysMouldController extends BaseController {

    @Autowired
    MsMouldDictService msMouldDictService;
    @Autowired
    MsMouldItemService msMouldItemService;
    @Autowired
    MsMouldMainService msMouldMainService;
    @Autowired
    TProviderPartnerService tProviderPartnerService;
    @Autowired
    VMaterialService vMaterialService;

    @ApiOperation(value = "字典列表")
    @RequestMapping(value = "/dictList", method = RequestMethod.POST)
    public CommonResult dictList(@Valid MsMouldDictQueryCriteria criteria, YmPageDto ymPage) {
        try {
            TableList<MsMouldDict> list = msMouldDictService.list(criteria, ymPage);
            return CommonResult.success(list, "获取成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "字典下拉列表")
    @RequestMapping(value = "/dictAllList", method = RequestMethod.POST)
    public CommonResult dictAllList(@Valid MsMouldDictQueryCriteria criteria) {
        try {
            List<MsMouldDict> list = msMouldDictService.queryAll(criteria);
            return CommonResult.success(list, "获取成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "字典保存")
    @RequestMapping(value = "/dictSave", method = RequestMethod.POST)
    public CommonResult dictSave(MsMouldDict dict) {
        try {
            msMouldDictService.saveOrUpdate(dict);
            return CommonResult.success();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "字典详情")
    @RequestMapping(value = "/dictDetail", method = RequestMethod.POST)
    public CommonResult dictDetail(@ApiParam(value = "数据主键") @RequestParam(required = true) String id) {
        try {
            MsMouldDict dict = msMouldDictService.getById(id);

            return CommonResult.success(dict);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "字典删除")
    @RequestMapping(value = "/dictDelete", method = RequestMethod.POST)
    public CommonResult dictDelete(@ApiParam(value = "数据主键") @RequestParam(required = true) String id) {
        try {
            msMouldDictService.removeById(id);
            return CommonResult.success();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "模具费用列表")
    @RequestMapping(value = "/mouldList", method = RequestMethod.POST)
    public CommonResult mouldList(@Valid MsMouldMainQueryCriteria criteria, YmPageDto ymPage) {
        try {
            TableList<MsMouldMain> list = msMouldMainService.list(criteria, ymPage);
            return CommonResult.success(list, "获取成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "添加费用")
    @RequestMapping(value = "/addMould", method = RequestMethod.POST)
    public CommonResult addMould(MsMouldMain mouldMain) {
        try {
            msMouldMainService.addMould(mouldMain);
            return CommonResult.success();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "修改费用")
    @RequestMapping(value = "/editMould", method = RequestMethod.POST)
    // @RequestBody
    public CommonResult editMould(MsMouldMain mouldMain) {
        try {
            msMouldMainService.editMould(mouldMain);
            return CommonResult.success();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "费用详情")
    @RequestMapping(value = "/detailMould", method = RequestMethod.POST)
    public CommonResult detailMould(MsMouldMain mouldMain) {
        try {
            MsMouldMain msMouldMain = msMouldMainService.detailMould(mouldMain.getMouldMainId());
            return CommonResult.success(msMouldMain);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "同步一次未生成的虚拟件采购单")
    @RequestMapping(value = "/autoCreateOrderFn", method = RequestMethod.POST)
    public CommonResult autoCreateOrderFn() {
        try {
             msMouldMainService.autoCreateOrderFn();
            return CommonResult.success();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }



    @ApiOperation(value = "删除物料关联")
    @RequestMapping(value = "/deleteMould", method = RequestMethod.POST)
    public CommonResult deleteMould(String mouldMainId) {
        try {
            msMouldMainService.deleteMould(mouldMainId);
            return CommonResult.success();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "删除物料关联")
    @RequestMapping(value = "/deleteMouldItem", method = RequestMethod.POST)
    public CommonResult deleteMouldItem(String mouldItemId) {
        try {
            msMouldItemService.removeById(mouldItemId);
            return CommonResult.success();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "获取模具配置的物料信息")
    @RequestMapping(value = "/getMouldItems", method = RequestMethod.POST)
    public CommonResult getMouldItems(String mouldMainId) {
        try {
            MsMouldItemQueryCriteria criteria = new MsMouldItemQueryCriteria();
            criteria.setMouldMainId(mouldMainId);
            List<MsMouldItem> msMouldItems = new ArrayList<>();
            if (UtilValidate.isNotEmpty(mouldMainId)) {
                msMouldItems = msMouldItemService.queryAll(criteria);
            }
            return CommonResult.success(msMouldItems);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "关闭费用")
    @RequestMapping(value = "/closeMould", method = RequestMethod.POST)
    public CommonResult editMould(String mouldMainId) {
        try {
            MsMouldMain mouldMain = new MsMouldMain();
            mouldMain.setMouldMainId(mouldMainId);
            mouldMain.setIsClose(1);
            msMouldMainService.updateById(mouldMain);
            return CommonResult.success();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "供应商下拉")
    @RequestMapping(value = "/supplierList", method = RequestMethod.POST)
    public CommonResult supplierList(@Valid TProviderPartnerQueryCriteria criteria) {
        try {
            List<TProviderPartner> list = tProviderPartnerService.queryAll(criteria);
            return CommonResult.success(list, "获取成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "供应商表格")
    @RequestMapping(value = "/supplierTableList", method = RequestMethod.POST)
    public CommonResult supplierTableList(@Valid TProviderPartnerQueryCriteria criteria, YmPageDto ymPageDto) {
        try {
            TableList<TProviderPartner> list = tProviderPartnerService.list(criteria, ymPageDto);
            return CommonResult.success(list, "获取成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "物料下拉")
    @RequestMapping(value = "/materialList", method = RequestMethod.POST)
    public CommonResult materialList(@Valid VMaterialQueryCriteria criteria) {
        try {
            List<VMaterial> list = vMaterialService.queryList(criteria);
            return CommonResult.success(list, "获取成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "物料列表")
    @RequestMapping(value = "/materialTableList", method = RequestMethod.POST)
    public CommonResult materialTableList(@Valid VMaterialQueryCriteria criteria, YmPageDto ymPage) {
        try {
            TableList<VMaterial> list = vMaterialService.list(criteria, ymPage);
            return CommonResult.success(list, "获取成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "退款单据查询列表")
    @RequestMapping(value = "/priceTableList", method = RequestMethod.POST)
    public CommonResult priceTableList(@Valid MsOrderListQueryCriteria criteria, YmPageDto ymPage) {
        try {
            // ymPage.setPage(ymPage.getPage()-1);
            TableList<DbMsOrderDto> list = tProviderPartnerService.getPriceTableList(criteria, ymPage);
            return CommonResult.success(list, "获取成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "导入")
    @RequestMapping(value = "/importMsOrderExcel", method = RequestMethod.POST)
    public CommonResult importMsOrderExcel(@RequestBody ImportMsOrderExcelDto dto) {
        try {
            if (UtilValidate.isEmpty(dto.getList())) {
                throw new YmException("数据不能为空！");
            }
            List<Map<String, Object>> maps = msMouldMainService.importMsOrder(dto);
            return CommonResult.success(maps);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "采购单列表")
    @RequestMapping(value = "/purchaseOrderTableList", method = RequestMethod.POST)
    public CommonResult purchaseOrderTableList(@Valid MsOrderListQueryCriteria criteria, YmPageDto ymPage) {
        try {
            // ymPage.setPage(ymPage.getPage()-1);
            TableList<MsOrderDto> list = tProviderPartnerService.purchaseOrderTableList(criteria, ymPage);
            return CommonResult.success(list, "获取成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "外协加工单列表")
    @RequestMapping(value = "/outsourceOrderTableList", method = RequestMethod.POST)
    public CommonResult outsourceOrderTableList(@Valid MsOrderListQueryCriteria criteria, YmPageDto ymPage) {
        try {
            // ymPage.setPage(ymPage.getPage()-1);
            TableList<MsOrderDto> list = tProviderPartnerService.outsourceOrderTableList(criteria, ymPage);
            return CommonResult.success(list, "获取成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "我的代表")
    @RequestMapping(value = "/waitList", method = RequestMethod.POST)
    public CommonResult waitList() {
        try {
            Integer myWaitList = msMouldMainService.getMyWaitList();
            Map<String, Object> map = new HashMap<>();
            map.put("mouldWait", myWaitList);
            return CommonResult.success(map, "获取成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

}
